<?php
class BuyAuthorizeService {
	private $_password = '123456';
	
	/*
	 * 二次密码
	 * */
	public function vaild($user, $password) {
		if (empty ( $user )) {
			return array ('status' => false, "message" => "请先登录!" );
		}
		
		if (empty ( $password )) {
			return array ('status' => false, 'message' => "密码不能为空" );
		}
		
		$sq = $this->getUser ( $user ['uid'] );
		
		if (empty ( $sq )) {
			return array ('status' => false, "message" => "尚未登记在册!" );
		}
		
		if ($sq ['num'] > 4) {
			return array ('status' => false, 'message' => "连续5次密码输错，账号已被禁用" );
		}
		
		if ($sq ['password'] !== md5 ( trim ( $password ) )) {
			$this->addFaildNum ( $user ['uid'] );
			$last = 5 - $sq ['num'] - 1;
			$message = '密码错误,你还剩' . $last . '次机会';
			return array ('status' => false, 'message' => $message );
		}
		
		if ($sq ['status'] < 1) {
			return array ('status' => false, 'message' => "尚未激活，请先激活密码" );
		}
		
		setcookie ( "jiami", $sq ['uid'], 0 );
		$this->clearFaildNum ( $sq ['uid'] );
		return array ('status' => true, "message" => "授权成功" );
	}
	
	public function save($user, $old, $new) {
		
		if (empty ( $user )) {
			return array ('status' => false, "message" => "请先登录!" );
		}
		
		if (empty ( $new ) || empty ( $old )) {
			return array ('status' => false, 'message' => "密码不能为空" );
		}
		
		if ($user ['verify'] != 1) {
			return array ('status' => false, 'message' => "你尚未实名认证" );
		}
		
		if ($new == "123456") {
			return array ('status' => false, 'message' => "新密码太简单了" );
		}
		
		if ($new == $old) {
			return array ('status' => false, 'message' => "新密码与旧密码相同了" );
		}
		
		if ($this->filter ( $new )) {
			return array ('status' => false, 'message' => "密码中请勿包含了符号" );
		}
		
		$sq = $this->getUser ( $user ['uid'] );
		
		if ($sq ['num'] > 4) {
			return array ('status' => false, 'message' => "输错5次，帐号已被禁用" );
		}
		
		if ($sq ['password'] !== md5 ( $old )) {
			$this->addFaildNum ( $user ['uid'] );
			$last = 5 - $sq [num] - 1;
			$message = '原密码错误,你还剩' . $last . '次机会';
			return array ('status' => false, 'message' => $message );
		
		}
		
		BuyPasswordModel::instance ()->savePasswordByUid ( md5 ( trim ( $new ) ), $user ['uid'] );
		$this->clearFaildNum ( $sq ['uid'] );
		return array ('status' => true, 'message' => "授权密码修改成功,请返回订购中心登录" );
	}
	
	private function clearFaildNum($uid) {
		BuyPasswordModel::instance ()->setNumByUid ( 0, $uid );
	}
	/**
	 * 检测是否包含非法字符
	 * @return boolean true or false
	 */
	private function filter($str) {
		$pattern = "#[\*\.\/\?\-\%\!]+#i";
		return preg_match ( $pattern, $str );
	}
	
	public function addFaildNum($uid) {
		BuyPasswordModel::instance ()->addFaildNum ( $uid );
	}
	
	public function addUser($uid) {
		//检查是否已经含有权限
		$user = BuyPasswordModel::instance ()->getUserByUid ( $uid );
		
		if (! empty ( $user )) {
			return "已经授权了";
		}
		$params = array ('uid' => $uid, 'password' => md5 ( $this->_password ) );
		$flag = BuyPasswordModel::instance ()->addUser ( $params );
		
		if ($flag > 0) {
			$rs = "授权成功";
		} else {
			$rs = "授权失败";
		}
		return $rs;
	}
	
	//初始化
	public function init($uid) {
		$rs = BuyPasswordModel::instance ()->savedUser ( array ('password' => md5 ( $this->_password ), 'num' => 0, 'status' => 0 ), $uid );
		return $rs > 0 ? "初始化成功" : "初始化失败";
	}
	
	public function getUser($uid) {
		if (empty ( $uid )) {
			return false;
		}
		return BuyPasswordModel::instance ()->getUserByUid ( $uid );
	}

}
